package cn.icare.cms.controller.restful;

import cn.icare.cms.ADService;
import cn.icare.cms.ADService;
import cn.icare.cms.model.ro.AddADRO;
import cn.icare.cms.model.ro.EditADRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/ad")
@Api(tags = "REST首页广告位")
public class ADController extends BaseController {
    @Resource
    private ADService ADService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/AD
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个广告", notes = "t_AD表新增单个广告")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddADRO ro, HttpServletRequest request) {
        String userName = this.getCurrentUserName(request);
        Integer id = this.ADService.createOne(ro, userName);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/AD
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个广告", notes = "usergroup表更新单个广告")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditADRO ro, HttpServletRequest request) {
        String userName = this.getCurrentUserName(request);
        if (this.ADService.updateOne(ro, userName)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/AD/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个广告", notes = "删除符合主键的单个广告")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Integer id) {
        if (this.ADService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/AD?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个广告", notes = "删除符合主键的多个广告")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Integer> ids) {
        if (this.ADService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/AD/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个广告", notes = "返回符合主键的单个广告")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.ADService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/AD?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个广告", notes = "user表返回符合条件的多个广告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", required = true
                    , value = "广告类型0-首页轮播广告 1-弹窗广告",
                    type = "Integer.class"),
            @ApiImplicitParam(name = "status", required = true
                    , value = "状态1-启用0-禁用 -1全部",
                    type = "Integer.class")
    })
    public ResponseEntity<RespMsgVO> getList(Integer type,Integer status) {
        return super.getRespOk(this.ADService.getAllList(type,status));
    }


    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddADRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditADRO ro, HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Integer> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Integer type,Integer status) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
